const api = require('../../../../ws_micro_shop/utils/api.js');
const tools = require('../../../../ws_micro_shop/utils/getuserinfo.js');
const app = getApp();

Page({
  data: {
    quick_list: [],
    goods_list: [],
    cat_goods_list: [],
    carGoods: [],
    showModal: false,
    checked: false,
    cat_checked: false,
    color: '',
    total: {
      total_price: 0.0,
      total_num: 0,
    },
    unit: '',
    page: 1,
    more: true,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: '暂无商品，商家正在准备中~',
    },
    loading: false,
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    oldindex: 0,
    imgUrl: app.imgUrl,
  },

  onLoad(opts) {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;

    // 判断当前页面在不在虚拟菜单中
    this.setData({
      useTabbar: that.data.useTabbar && app.getTabBarStatus(),
    });

    let options = opts || {};
    app.tabbarindex = options.tabbarindex
      ? options.tabbarindex - 0
      : app.tabbarindex;
    let userinfo = wx.getStorageSync('user_info');
    if (options && options.shopid) {
      app.ws_micro_shop_globalData.shopid = options.shopid;
    }
    if (Number(userinfo.info) === 0) {
      tools.onGetInfo(this, userinfo.auth_mode);
    }
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    app.pageOnLoad(this);
    this.setData({
      store: wx.getStorageSync('store'),
      opts: options,
    });
    this.getQuickCat();
  },

  getQuickCat() {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.quick.quick_cat,
      success: function(res) {
        if (res.code === 0) {
          let act_list = res.data.cat_list;
          let aid = 0;
          if (act_list.length > 0) {
            aid = act_list[0].id || 0;
            act_list[0].cat_checked = true;
            that.setData({
              quick_list: act_list || [],
              selectedMenuId: aid,
            });
          }
          that.setData({
            quick_top_picture: res.data.quick_top_picture || '',
          });
          that.loadCatGoods(aid);
        }
      },
    });
  },

  loadCatGoods(id) {
    let that = this;
    let carGoods = that.data.carGoods;
    let page = that.data.page;
    let list = that.data.cat_goods_list;
    if (that.data.more) {
      wx.showLoading({
        title: '加载中...',
        mask: true,
      });
      app.ws_micro_shoprequest({
        url: api.quick.quick_goods,
        data: {
          cat_id: id,
          page,
          limit: 10,
        },
        success: function(res) {
          wx.hideLoading();
          if (res.code === 0) {
            let data = res.data.list;
            if (data.length > 0) {
              if (carGoods.length > 0) {
                for (let i = 0; i < carGoods.length; i++) {
                  for (let j = 0; j < data.length; j++) {
                    if (Number(carGoods[i].goods_id) === Number(data[j].id)) {
                      data[j].num += carGoods[i].num;
                      continue;
                    }
                  }
                }
              }
              page += 1;
              that.setData({
                cat_goods_list: list.concat(data),
                page,
              });
            } else {
              that.setData({
                more: false,
              });
              // wx.showToast({
              //   title: '没有更多数据了',
              //   icon: 'none',
              // });
            }
            that.setData({
              loading: true,
            });
          }
        },
      });
    }
  },

  loadMoreGoods() {
    let id = this.data.selectedMenuId;
    this.loadCatGoods(id);
  },

  selectMenu(event) {
    let data = event.currentTarget.dataset;
    let { quick_list, oldindex } = this.data;
    let cat_checked;
    let index = data.index;
    if (Number(oldindex) === Number(index)) return;
    let quick_list_length = quick_list.length;
    for (let a = 0; a < quick_list_length; a++) {
      quick_list[a]['cat_checked'] = false;
      if (Number(quick_list[a]['id']) === Number(quick_list[index]['id'])) {
        quick_list[a]['cat_checked'] = true;
      }
    }
    cat_checked = false;
    this.setData({
      selectedMenuId: data.id,
      quick_list: quick_list,
      cat_checked: cat_checked,
      more: true,
      page: 1,
      cat_goods_list: [],
      loading: false,
      oldindex: index,
    });
    this.loadCatGoods(data.id || 0);
  },

  onShareAppMessage() {
    let that = this;
    let user_info = wx.getStorageSync('user_info');
    app.shareSendCoupon(that);
    return {
      path:
        '/ws_micro_shop/pages/index/index?user_id=' +
        user_info.id +
        '&tabbarindex=' +
        app.tabbarindex +
        '&shopid=' +
        app.ws_micro_shop_globalData.shopid,
    };
  },
  onShareTimeline() {
    let that = this;
    let user_info = wx.getStorageSync('user_info');
    app.shareSendCoupon(that);
    return {
      title: '',
      imageUrl: '',
      query: 'user_id=' +
      user_info.id +
      '&tabbarindex=' +
      app.tabbarindex +
      '&shopid=' +
      app.ws_micro_shop_globalData.shopid
    }
  },

  jia(e) {
    let data = e.currentTarget.dataset;
    let member_only = data.member_only;
    let userInfo = wx.getStorageSync('user_info');
    if (Number(member_only) === 1 && userInfo && userInfo.level === -1) {
      wx.showModal({
        title: '提示',
        content: '您还不是会员,不能购买此商品',
        showCancel: false,
        success(res) {},
      });
      return false;
    }
    let currGoods = null;
    let goods_length;
    let length;
    let max = false;
    let total = this.data.total;
    let goods_cat_all = this.data.cat_goods_list;
    goods_length = goods_cat_all.length;
    for (let a = 0; a < goods_length; a++) {
      if (Number(goods_cat_all[a]['id']) === Number(data.id)) {
        let buyNum = goods_cat_all[a]['num'];
        let maxNum = JSON.parse(goods_cat_all[a].attr)[0].num;
        let limitNum = goods_cat_all[a]['limit_count'];
        let already = goods_cat_all[a]['user_order_count'];
        if (buyNum >= maxNum) {
          goods_cat_all[a].num = maxNum;
          max = true;
          wx.showToast({
            title: '商品库存不足',
            icon: 'none',
          });
          return;
        } else {
          if (limitNum > 0) {
            let able_num = limitNum - already.cart - already.order;
            if (goods_cat_all[a].num < able_num) {
              goods_cat_all[a].num += 1;
            } else {
              wx.showToast({
                title: '可购买最大数量为' + able_num,
                icon: 'none',
              });
              return;
            }
          } else {
            goods_cat_all[a].num += 1;
          }
        }
        currGoods = goods_cat_all[a];
        break;
      }
    }
    let goods_price = Number(currGoods.price) * Number(currGoods.num);
    let carGoods = this.data.carGoods;
    let good = {
      goods_id: currGoods.id,
      num: 1,
      goods_name: currGoods.name,
      goods_pic: currGoods.cover_pic,
      attr: '',
      attr1: JSON.parse(currGoods.attr),
      goods_price: Number(goods_price),
      price: currGoods.price,
      limit_count: currGoods.limit_count,
      user_order_count: currGoods.user_order_count,
    };
    length = carGoods.length;
    let flag = true;
    currGoods.price = Number(currGoods.price);
    total.total_price = Number(total.total_price);
    if (length <= 0) {
      carGoods.push(good);
      total.total_num += 1;
      total.total_price += Number(currGoods.price);
    } else {
      for (let a = 0; a < length; a++) {
        if (Number(carGoods[a]['goods_id']) === Number(currGoods.id)) {
          if (max === false) {
            carGoods[a]['num'] += 1;
            carGoods[a]['goods_price'] = Number(goods_price);
            total.total_num += 1;
            total.total_price += Number(currGoods.price);
          }
          flag = false;
        }
      }
      if (flag) {
        carGoods.push(good);
        total.total_num += 1;
        total.total_price += Number(currGoods.price);
      }
    }
    currGoods.price = Number(currGoods.price).toFixed(2);
    total.total_price = Number(total.total_price).toFixed(2);
    this.setData({
      cat_goods_list: goods_cat_all,
      total: total,
      carGoods: carGoods,
    });
  },

  jian(e) {
    let data = e.currentTarget.dataset;
    let currGoods = null;
    let goods_cat_all = this.data.cat_goods_list;
    let goods_length = goods_cat_all.length;
    for (let a = 0; a < goods_length; a++) {
      if (Number(goods_cat_all[a]['id']) === Number(data.id)) {
        goods_cat_all[a].num -= 1;
        currGoods = goods_cat_all[a];
        break;
      }
    }
    let total = this.data.total;
    total.total_num -= 1;
    currGoods.price = Number(currGoods.price);
    total.total_price = Number(total.total_price);
    total.total_price -= Number(currGoods.price);
    total.total_price = total.total_price.toFixed(2);
    currGoods.price = currGoods.price.toFixed(2);
    this.setData({
      cat_goods_list: goods_cat_all,
      total: total,
    });
    let carGoods = this.data.carGoods;
    let car_goods = carGoods.find(function(v) {
      return v.goods_id === data.id;
    });
    car_goods.num -= 1;
    if (car_goods.num === 0) {
      carGoods.splice(carGoods.indexOf(car_goods), 1);
    } else {
      car_goods.goods_price = Number(car_goods.goods_price);
      car_goods.goods_price -= currGoods.price;
      car_goods.goods_price = car_goods.goods_price.toFixed(2);
    }
    this.setData({
      carGoods: carGoods,
    });
  },

  showDialogBtn(e) {
    let that = this;
    let data = e.currentTarget.dataset;
    let member_only = data.member_only;
    let userInfo = wx.getStorageSync('user_info');
    if (Number(member_only) === 1 && userInfo && userInfo.level === -1) {
      wx.showModal({
        title: '提示',
        content: '您还不是会员,不能购买此商品',
        showCancel: false,
        success(res) {},
      });
      return false;
    }
    let goods_id = data.id;
    let goods;
    let goods_list = that.data.cat_goods_list;
    app.ws_micro_shoprequest({
      url: api.default.goods,
      data: {
        id: goods_id,
      },
      success: function(res) {
        if (res.code === 0) {
          that.setData({
            data: data,
            attr_group_list: res.data.attr_group_list,
            showModal: true,
          });
        }
      },
    });
    goods = goods_list.find(function(v) {
      return Number(v.id) === Number(data.id);
    });
    let attr = JSON.parse(goods.attr);
    let length = attr.length;
    for (let a = 0; a < length; a++) {
      attr[a]['check_num'] = 0;
    }
    that.setData({
      goods: goods,
      goods_name: goods.name,
      attr: attr,
    });
    let car_goods = that.data.carGoods;
    let car_goods_length = car_goods.length;
    if (car_goods_length >= 1) {
      for (let a = 0; a < car_goods_length; a++) {
        if (
          JSON.stringify(goods.name) !==
          JSON.stringify(car_goods[a]['goods_name'])
        ) {
          that.setData({
            check_num: false,
            check_goods_price: false,
          });
        }
      }
    }
  },

  close(e) {
    this.setData({
      showModal: false,
      check_num: false,
      check_attr_list: [],
      check_goods_price: false,
    });
  },

  attrClick: function(e) {
    let that = this;
    let attr_group_id = e.target.dataset.groupId;
    let attr_id = e.target.dataset.id;
    let attr_group_list = this.data.attr_group_list;
    for (let i in attr_group_list) {
      if (Number(attr_group_list[i].attr_group_id) !== Number(attr_group_id)) {
        continue;
      }
      for (let j in attr_group_list[i].attr_list) {
        if (
          Number(attr_group_list[i].attr_list[j].attr_id) === Number(attr_id)
        ) {
          attr_group_list[i].attr_list[j].checked = true;
        } else {
          attr_group_list[i].attr_list[j].checked = false;
        }
      }
    }
    that.setData({
      attr_group_list,
    });
    let checked_attr_list = [];
    let check_all = true;
    for (let i in attr_group_list) {
      if (Object.prototype.hasOwnProperty.call(attr_group_list, i)) {
        let group_checked = false;
        for (let j in attr_group_list[i].attr_list) {
          if (attr_group_list[i].attr_list[j].checked) {
            checked_attr_list.push(attr_group_list[i].attr_list[j].attr_id);
            group_checked = true;
            break;
          }
        }
        if (!group_checked) {
          check_all = false;
          break;
        }
      }
    }
    if (check_all) {
      let goods = that.data.goods;
      wx.showLoading({
        title: '正在加载',
        mask: true,
      });
      app.ws_micro_shoprequest({
        url: api.default.goods_attr_info,
        data: {
          goods_id: goods.id,
          attr_list: JSON.stringify(checked_attr_list),
        },
        success: function(res) {
          wx.hideLoading();
          // that.setData({
          //   loading: false,
          // });
          if (res.code === 0) {
            goods.limit_count = res.data.limit_count;
            goods.min_num = Number(res.data.min_num);
            goods.user_order_count = res.data.user_order_count;
            goods.oriprice = res.data.oriprice ? res.data.oriprice : 0;
            that.setData({
              goods,
            });
          }
        },
      });
    }
    let attr_group_list_length = attr_group_list.length;
    let check_attr_list = [];
    let check_attr_lists = [];
    for (let a = 0; a < attr_group_list_length; a++) {
      let attr_group_list_check = attr_group_list[a]['attr_list'];
      let attr_group_list_check_length = attr_group_list_check.length;
      for (let i = 0; i < attr_group_list_check_length; i++) {
        if (attr_group_list_check[i]['checked'] === true) {
          let attrs = {
            attr_group_id: attr_group_list[a]['attr_group_id'],
            attr_group_name: attr_group_list[a]['attr_group_name'],
            attr_id: attr_group_list_check[i]['attr_id'],
            attr_name: attr_group_list_check[i]['attr_name'],
          };
          check_attr_lists.push(attrs);
          let attrs_list = {
            attr_id: attr_group_list_check[i]['attr_id'],
            attr_name: attr_group_list_check[i]['attr_name'],
          };
          check_attr_list.push(attrs_list);
        }
      }
    }
    let attr = this.data.attr;
    let attr_length = attr.length;
    let check_goods_price;
    for (let x = 0; x < attr_length; x++) {
      if (
        JSON.stringify(attr[x]['attr_list']) === JSON.stringify(check_attr_list)
      ) {
        check_goods_price = attr[x]['price'];
      }
    }
    this.setData({
      attr_group_list: attr_group_list,
      check_goods_price: check_goods_price ? check_goods_price : '',
      check_goods_total_price: check_goods_price,
      check_attr_list: check_attr_list,
    });
    let carGoods = this.data.carGoods;
    let length = this.data.carGoods.length;
    let goods = this.data.goods;
    let check_num = 0;
    for (let a = 0; a < length; a++) {
      if (
        Number(carGoods[a]['goods_id']) === Number(goods.id) &&
        JSON.stringify(carGoods[a]['attr']) === JSON.stringify(check_attr_lists)
      ) {
        check_num = carGoods[a].num;
        break;
      }
    }
    this.setData({
      check_num: check_num,
    });
  },

  onConfirm(e) {
    let attr_group_lists = this.data.attr_group_list;
    let checked_attr_list = [];
    for (let i in attr_group_lists) {
      if ({}.hasOwnProperty.call(attr_group_lists, i)) {
        let attr = false;
        for (let j in attr_group_lists[i].attr_list) {
          if (attr_group_lists[i].attr_list[j].checked) {
            attr = {
              attr_id: attr_group_lists[i].attr_list[j].attr_id,
              attr_name: attr_group_lists[i].attr_list[j].attr_name,
            };
            break;
          }
        }
        if (!attr) {
          wx.showToast({
            title: '请选择' + attr_group_lists[i].attr_group_name,
            icon: 'none',
          });
          return true;
        } else {
          checked_attr_list.push({
            attr_group_id: attr_group_lists[i].attr_group_id,
            attr_group_name: attr_group_lists[i].attr_group_name,
            attr_id: attr.attr_id,
            attr_name: attr.attr_name,
          });
        }
      }
    }
    this.setData({
      attr_group_list: attr_group_lists,
    });
    let attr = this.data.attr;
    let check_attr_list = this.data.check_attr_list;
    let attr_length = attr.length;
    let check_goods_num;
    for (let x = 0; x < attr_length; x++) {
      if (
        JSON.stringify(attr[x]['attr_list']) === JSON.stringify(check_attr_list)
      ) {
        check_goods_num = attr[x]['num'];
      }
    }
    // 数量+
    let data = this.data.data;
    let goods_all = this.data.cat_goods_list;
    let goods_all_length = goods_all.length;
    let monery;
    let quickgoods = [];
    for (let x = 0; x < goods_all_length; x++) {
      if (Number(goods_all[x]['id']) === Number(data.id)) {
        quickgoods.push(goods_all[x]);
      }
    }
    this.setData({
      checked_attr_list: checked_attr_list,
    });
    attr_length = checked_attr_list.length;
    let attr_id = [];
    for (let a = 0; a < attr_length; a++) {
      attr_id.push(checked_attr_list[a]['attr_id']);
    }
    let carGoods = this.data.carGoods;
    let check_goods_price = Number(this.data.check_goods_price);
    if (check_goods_price === 0) {
      monery = Number(quickgoods[0].price);
    } else {
      monery = Number(check_goods_price);
    }
    let current = this.data.goods;
    let good = {
      goods_id: quickgoods[0].id,
      num: 1,
      goods_name: quickgoods[0].name,
      goods_pic: quickgoods[0].cover_pic,
      attr: checked_attr_list,
      attr1: attr,
      goods_price: monery,
      price: monery,
      limit_count: current.limit_count,
      user_order_count: current.user_order_count,
    };
    let length = carGoods.length;
    let flag = true;
    let check_num = 0;
    let able_num =
      current.limit_count -
      current.user_order_count.cart -
      current.user_order_count.order;
    if (length <= 0) {
      if (current.limit_count > 0) {
        if (0 < able_num) {
          check_num = 1;
          carGoods.push(good);
        } else {
          wx.showToast({
            title: '可购买最大数量为' + able_num,
            icon: 'none',
          });
          return;
        }
      } else {
        check_num = 1;
        carGoods.push(good);
      }
    } else {
      for (let a = 0; a < length; a++) {
        if (
          Number(carGoods[a]['goods_id']) === Number(quickgoods[0].id) &&
          JSON.stringify(carGoods[a]['attr']) ===
            JSON.stringify(checked_attr_list)
        ) {
          if (current.limit_count > 0) {
            if (carGoods[a]['num'] < able_num) {
              carGoods[a]['num'] += 1;
              check_num = carGoods[a]['num'];
              flag = false;
            } else {
              wx.showToast({
                title: '可购买最大数量为' + able_num,
                icon: 'none',
              });
              return;
            }
          } else {
            carGoods[a]['num'] += 1;
            check_num = carGoods[a]['num'];
            flag = false;
          }
        }
      }
      if (flag) {
        if (current.limit_count > 0) {
          if (0 < able_num) {
            carGoods.push(good);
            check_num = 1;
          } else {
            wx.showToast({
              title: '可购买最大数量为' + able_num,
              icon: 'none',
            });
            return;
          }
        } else {
          carGoods.push(good);
          check_num = 1;
        }
      }
    }
    if (check_num > check_goods_num) {
      wx.showToast({
        title: '商品库存不足',
        icon: 'none',
      });
      check_num = check_goods_num;
      for (let a = 0; a < length; a++) {
        if (
          Number(carGoods[a]['goods_id']) === Number(quickgoods[0].id) &&
          JSON.stringify(carGoods[a]['attr']) ===
            JSON.stringify(checked_attr_list)
        ) {
          carGoods[a]['num'] = check_goods_num;
        }
      }
      !this.data.check_num && carGoods.splice(-1, 1);
      return;
    }
    for (let a = 0; a < length; a++) {
      if (
        Number(carGoods[a]['goods_id']) === Number(quickgoods[0].id) &&
        JSON.stringify(carGoods[a]['attr']) ===
          JSON.stringify(checked_attr_list)
      ) {
        carGoods[a]['goods_price'] = Number(carGoods[a]['goods_price']);
        carGoods[a]['goods_price'] += monery;
        carGoods[a]['goods_price'] = carGoods[a]['goods_price'].toFixed(2);
      }
    }
    let goods_length = quickgoods.length;
    for (let b = 0; b < goods_length; b++) {
      quickgoods[b].num += 1;
    }
    let total = this.data.total;
    total.total_num += 1;
    total.total_price = Number(total.total_price);
    total.total_price += Number(monery);
    total.total_price = total.total_price.toFixed(2);
    this.setData({
      cat_goods_list: goods_all,
      carGoods: carGoods,
      total: total,
      check_num: check_num,
      check_goods_total_price: (monery * Number(check_num)).toFixed(2),
    });
  },

  hideModal() {
    this.setData({
      showModal: false,
      check_attr_list: [],
      check_goods_price: false,
    });
  },

  guigejian() {
    let goods = this.data.goods;
    let goods_id = goods.id;
    let goods_list = this.data.cat_goods_list;
    let goods_list_length = goods_list.length;
    let check_goods = [];
    for (let b = 0; b < goods_list_length; b++) {
      if (Number(goods.id) === Number(goods_list[b].id)) {
        check_goods.push(goods_list[b]);
      }
    }
    let check_goods_length = check_goods.length;
    for (let x = 0; x < check_goods_length; x++) {
      check_goods[x].num -= 1;
    }
    this.setData({
      cat_goods_list: goods_list,
    });
    let attr_group_lists = this.data.attr_group_list;
    let checked_attr_list = [];
    for (let i in attr_group_lists) {
      if ({}.hasOwnProperty.call(attr_group_lists, i)) {
        let attr = false;
        for (let j in attr_group_lists[i].attr_list) {
          if (attr_group_lists[i].attr_list[j].checked) {
            attr = {
              attr_id: attr_group_lists[i].attr_list[j].attr_id,
              attr_name: attr_group_lists[i].attr_list[j].attr_name,
            };
            break;
          }
        }
        if (!attr) {
          wx.showToast({
            title: '请选择' + attr_group_lists[i].attr_group_name,
            icon: 'none',
          });
          return true;
        } else {
          checked_attr_list.push({
            attr_group_id: attr_group_lists[i].attr_group_id,
            attr_group_name: attr_group_lists[i].attr_group_name,
            attr_id: attr.attr_id,
            attr_name: attr.attr_name,
          });
        }
      }
    }
    let check_num = this.data.check_num;
    let carGoods = this.data.carGoods;
    let length = carGoods.length;
    for (let a = 0; a < length; a++) {
      if (
        Number(carGoods[a]['goods_id']) === Number(goods_id) &&
        JSON.stringify(carGoods[a]['attr']) ===
        JSON.stringify(checked_attr_list)
      ) {
        carGoods[a]['num'] -= 1;
        if (carGoods[a]['num'] === 0) {
          carGoods.splice(a, 1);
          check_num = false;
          break;
        } else {
          carGoods[a]['goods_price'] = (
            carGoods[a]['num'] * carGoods[a]['price']
          ).toFixed(2);
          check_num = carGoods[a]['num'];
        }
      }
    }
    this.setData({
      carGoods: carGoods,
      check_num: check_num,
    });
    let total = this.data.total;
    total.total_num -= 1;
    let check_goods_price = this.data.check_goods_price;
    check_goods_price = Number(check_goods_price);
    total.total_price = Number(total.total_price);
    total.total_price -= check_goods_price;
    total.total_price = total.total_price.toFixed(2);
    this.setData({
      total: total,
      check_goods_total_price: (check_goods_price * Number(check_num)).toFixed(2),
    });
    if (total.total_num === 0) {
      this.setData({
        goodsModel: false,
      });
    }
  },

  goodsModel() {
    let goodsModel = this.data.goodsModel;
    if (!goodsModel) {
      this.setData({
        goodsModel: true,
        showModal: false,
      });
    } else {
      this.setData({
        goodsModel: false,
      });
    }
  },

  hideGoodsModel() {
    this.setData({
      goodsModel: false,
    });
  },

  tianjia(e) {
    let data = e.currentTarget.dataset;
    let { cat_goods_list: quick_list_goods = [], carGoods: carGoods_list = [] } = this.data;
    let check_goods_length;
    let check_goods_num;
    let quick_list_goods_length = quick_list_goods.length;
    let check_goods = [];
    for (let b = 0; b < quick_list_goods_length; b++) {
      if (Number(quick_list_goods[b].id) === Number(data.id)) {
        check_goods.push(quick_list_goods[b]);
      }
    }
    let check_cargoods_length = carGoods_list.length;
    let check_cargoods = [];
    for (let b = 0; b < check_cargoods_length; b++) {
      if (Number(carGoods_list[b].goods_id) === Number(data.id)) {
        check_cargoods.push(carGoods_list[b]);
      }
    }
    let goods_attr = check_cargoods[0]['attr'];
    if (!goods_attr) {
      let car_goods = carGoods_list.find(function(v) {
        return Number(v.goods_id) === Number(data.id);
      });
      if (car_goods.num < car_goods['attr1'][0].num) {
        if (car_goods.limit_count > 0) {
          let able_num =
            car_goods.limit_count -
            car_goods.user_order_count.cart -
            car_goods.user_order_count.order;
          if (car_goods.num < able_num) {
            car_goods['num'] += 1;
          } else {
            wx.showToast({
              title: '可购买最大数量为' + able_num,
              icon: 'none',
            });
            return;
          }
        } else {
          car_goods['num'] += 1;
        }
      } else {
        wx.showToast({
          title: '商品库存不足',
          icon: 'none',
        });
        return;
      }
      car_goods['goods_price'] = Number(car_goods['goods_price']);
      data.price = Number(data.price);
      car_goods['goods_price'] += data.price;
      car_goods['goods_price'] = car_goods['goods_price'].toFixed(2);
      check_goods_length = check_goods.length;
      for (let x = 0; x < check_goods_length; x++) {
        check_goods[x].num += 1;
      }
    } else {
      check_goods_length = check_goods.length;
      let carGoods_list_length = carGoods_list.length;
      let check_attr_list = [];
      for (let a = 0; a < carGoods_list_length; a++) {
        if (Number(data.index) === a) {
          let check_attr = carGoods_list[a]['attr'];
          let check_attr_length = check_attr.length;
          for (let i = 0; i < check_attr_length; i++) {
            let attrs_list = {
              attr_id: check_attr[i]['attr_id'],
              attr_name: check_attr[i]['attr_name'],
            };
            check_attr_list.push(attrs_list);
          }
          //购物车逻辑优化 by zlh 2020.07.01
          let check_attr1 = carGoods_list[a]['attr1'];
          let attr_length = check_attr1.length;
          for (let i = 0; i < attr_length; i++) {
            if (
              JSON.stringify(check_attr1[i]['attr_list']) ===
              JSON.stringify(check_attr_list)
            ) {
              check_goods_num = check_attr1[i]['num'];
            }
          }
        }
      }
      // let attr_length = goods_attr.length;
      // for (let i = 0; i < attr_length; i++) {
      //   if (
      //     JSON.stringify(goods_attr[i]['attr_list']) ===
      //     JSON.stringify(check_attr_list)
      //   ) {
      //     check_goods_num = goods_attr[i]['num'];
      //   }
      // }
      for (let a = 0; a < carGoods_list_length; a++) {
        if (Number(data.index) === a) {
          if (carGoods_list[a]['limit_count'] > 0) {
            let able_num =
              carGoods_list[a]['limit_count'] -
              carGoods_list[a]['user_order_count']['cart'] -
              carGoods_list[a]['user_order_count']['order'];
            if (carGoods_list[a]['num'] < able_num) {
              for (let x = 0; x < check_goods_length; x++) {
                check_goods[x].num += 1;
              }
              carGoods_list[a]['num'] += 1;
            } else {
              wx.showToast({
                title: '可购买最大数量为' + able_num,
                icon: 'none',
              });
              return;
            }
          } else {
            for (let x = 0; x < check_goods_length; x++) {
              check_goods[x].num += 1;
            }
            carGoods_list[a]['num'] += 1;
          }
          carGoods_list[a]['goods_price'] = Number(
            carGoods_list[a]['goods_price']
          );
          data.price = Number(data.price);
          carGoods_list[a]['goods_price'] += data.price;
          carGoods_list[a]['goods_price'] = carGoods_list[a][
            'goods_price'
          ].toFixed(2);

          if (carGoods_list[a]['num'] > check_goods_num) {
            wx.showToast({
              title: '商品库存不足',
              icon: 'none',
            });
            carGoods_list[a]['num'] -= 1;
            check_goods_length = check_goods.length;
            for (let x = 0; x < check_goods_length; x++) {
              check_goods[x].num -= 1;
            }
            carGoods_list[a]['goods_price'] -= data.price;
            carGoods_list[a]['goods_price'] = carGoods_list[a][
              'goods_price'
            ].toFixed(2);
            return;
          }
        }
      }
    }
    let total = this.data.total;
    total.total_num += 1;
    total.total_price = Number(total.total_price);
    data.price = Number(data.price);
    total.total_price += Number(data.price);
    total.total_price = total.total_price.toFixed(2);
    this.setData({
      cat_goods_list: quick_list_goods,
      carGoods: carGoods_list,
      total: total,
    });
  },

  jianshao(e) {
    let data = e.currentTarget.dataset;
    let carGoods = this.data.carGoods;
    let carGood = this.data.cat_goods_list;
    let quick_list_car_goods = [];
    let carGood_length = carGood.length;

    const currentGood = carGoods[data.index];
    if (currentGood.num > 0) {
      currentGood.num -= 1;
      if (currentGood.num === 0) {
        carGoods.splice(data.index, 1);
      } else {
        data.price = Number(data.price);
        currentGood.goods_price = Number(currentGood.goods_price);
        currentGood.goods_price -= data.price;
        currentGood.goods_price = currentGood.goods_price.toFixed(2);
      }
    }
    this.setData({
      carGoods: carGoods,
    });

    for (let a = 0; a < carGood_length; a++) {
      if (Number(data.id) === Number(carGood[a]['id'])) {
        quick_list_car_goods.push(carGood[a]);
      }
    }
    let quick_list_car_goods_length = quick_list_car_goods.length;
    for (let b = 0; b < quick_list_car_goods_length; b++) {
      if (Number(quick_list_car_goods[b].id) === Number(data.id)) {
        quick_list_car_goods[b].num -= 1;
      }
    }
    this.setData({
      cat_goods_list: carGood,
    });
    let total = this.data.total;
    total.total_num -= 1;
    total.total_price = Number(total.total_price);
    total.total_price -= Number(data.price);
    total.total_price = total.total_price.toFixed(2);
    this.setData({
      total: total,
    });
    if (total.total_num === 0) {
      this.setData({
        goodsModel: false,
      });
    }
  },

  clearCar(e) {
    let goods_list = this.data.cat_goods_list;
    let length = goods_list.length;
    let total = this.data.total;
    let check_num = this.data.check_num;
    for (let i = 0; i < length; i++) {
      goods_list[i]['num'] = 0;
    }
    this.setData({
      cat_goods_list: goods_list,
    });
    this.setData({ carGoods: [] });
    total.total_num = 0;
    total.total_price = 0;
    this.setData({
      total: total,
    });
    check_num = 0;
    this.setData({
      check_num: check_num,
      goodsModel: false,
    });
  },

  buynow() {
    let carGoods = this.data.carGoods;
    let quick_list = this.data.quick_list;
    wx.setStorageSync('quick_list', quick_list);

    carGoods = this.data.carGoods;
    wx.setStorageSync('carGoods', carGoods);

    let total = this.data.total;
    wx.setStorageSync('total', total);

    let check_num = this.data.check_num;
    wx.setStorageSync('check_num', check_num);

    let quick_hot_goods_lists = this.data.quick_hot_goods_lists;
    wx.setStorageSync('quick_hot_goods_lists', quick_hot_goods_lists);

    this.setData({
      goodsModel: false,
    });

    let length = carGoods.length;
    let cart_list = [];
    let cart_list_goods = [];
    for (let a = 0; a < length; a++) {
      if (Number(carGoods[a].num) !== 0) {
        cart_list_goods = {
          id: carGoods[a].goods_id,
          num: carGoods[a].num,
          attr: carGoods[a].attr,
        };
        cart_list.push(cart_list_goods);
      }
    }
    wx.navigateTo({
      url:
        '/ws_micro_shop/pages/order-submit/order-submit?cart_list=' +
        JSON.stringify(cart_list),
    });
  },

  collectFormId(e) {
    app.collectFormId(e);
  },

  imagePreview(e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.src],
    });
  },

  checkGoodDetail(e) {
    app.util.navigateTo('/ws_micro_shop/pages/goods/goods', {
      id: e.currentTarget.dataset.goodsId,
    });
  },
});
